package com.epam.rd.mycollections;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.RandomAccess;

public class MyArrayList<E> implements MyList<E>, RandomAccess {

    private static final String INDEX_ERROR_MESSAGE = "Index incorrect ";
    private static final int INITIAL_CAPACITY = 10;

    private int size;
    private Object[] elements;

    public MyArrayList() {
        elements = new Object[INITIAL_CAPACITY];
    }

    public MyArrayList(MyList<? extends E> list) {
        elements = list.toArray();
        size = list.size();
    }

    public MyArrayList(int initialCapacity) {
        if (initialCapacity < 0) {
            throw new MyIndexOutOfBoundsException(INDEX_ERROR_MESSAGE
                    + initialCapacity);
        }
        elements = new Object[initialCapacity];
    }

    public void ensureCapacity(int minCapacity) {
        if (minCapacity > elements.length) {
            Object[] temp = new Object[minCapacity];
            System.arraycopy(elements, 0, temp, 0, size);
            elements = temp;
        }
    }

    private void capacityUp(int forHow) {
        int newCapacity;
        if (size == elements.length) {
            newCapacity = (size >> 1) + size;
            if (newCapacity < size + forHow) {
                newCapacity = size + forHow;
            }
            ensureCapacity(newCapacity);
        }
    }

    public void trimToSize() {
        if (size < elements.length) {
            Object[] temp = new Object[size];
            System.arraycopy(elements, 0, temp, 0, size);
            elements = temp;
        }
    }

    @Override
    public void add(E el) {
        capacityUp(1);
        elements[size] = el;
        size++;
    }

    @Override
    public void add(int index, E el) {
        capacityUp(1);
        rangeCheckAdd(index);
        System.arraycopy(elements, index, elements, index + 1, size - index);
        elements[index] = el;
        size++;
    }

    @Override
    public void addAll(E[] array) {
        capacityUp(array.length);
        System.arraycopy(array, 0, elements, size, array.length);
        size += array.length;
    }

    @Override
    public void addAll(int index, E[] array) {
        rangeCheckAdd(index);
        capacityUp(array.length);
        System.arraycopy(elements, index, elements, index + array.length,
                size - index);
        System.arraycopy(array, 0, elements, index, array.length);
        size += array.length;
    }

    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            throw new MyIndexOutOfBoundsException(INDEX_ERROR_MESSAGE + index);
        }
    }

    private void rangeCheckAdd(int index) {
        if (index < 0 || index > size) {
            throw new MyIndexOutOfBoundsException(INDEX_ERROR_MESSAGE + index);
        }
    }

    @Override
    public E get(int index) {
        rangeCheck(index);
        return (E) elements[index];
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        E result = (E) elements[index];
        System.arraycopy(elements, index + 1, 
                elements, index, size - index - 1);
        elements[size - 1] = null;
        size--;
        return result;
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void set(int index, E el) {
        rangeCheck(index);
        elements[index] = el;
    }

    @Override
    public int size() {
        return size;
    }
    
    @Override
    public int indexOf(Object obj) {
        int result = -1;
        for (int i = 0; i < size; i++) {
            if (Objects.equals(elements[i], obj)) {
                result = i;
                break;
            }
        }
        return result;
    }

    @Override
    public Object[] toArray() {
        Object[] result = new Object[size];
        System.arraycopy(elements, 0, result, 0, size);
        return result;
    }

    @Override
    public Iterator<E> iterator() {
        return new DirectIterator();
    }

    private class DirectIterator implements Iterator<E> {
        private int next = 0;
        private int current = -1;

        @Override
        public boolean hasNext() {
            return next < size;
        }

        @Override
        public E next() {
            if (next >= size) {
                throw new NoSuchElementException();
            }
            current = next;
            return (E) elements[next++];
        }

        @Override
        public void remove() {
            if (current < 0) {
                throw new IllegalStateException();
            }
            MyArrayList.this.remove(current);
            next--;
            current = -1;
        }
    }

}
